import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import TaskModel 1.0

Rectangle {
    width: ListView.view.width
    height: 120
    color: index % 2 === 0 ? "#f0f0f0" : "#ffffff"
    border.color: "#cccccc"
    radius: 5

    required property int index
    required property string name
    required property int executeCount
    required property var dayPoints

    property TaskModel kidTaskModel

    signal taskCompleted(int points)

    RowLayout {
        anchors.fill: parent
        anchors.margins: 10
        spacing: 10

        ColumnLayout {
            Layout.fillWidth: true
            spacing: 5

            Label {
                text: "任务: " + name
                font.bold: true
            }

            Label {
                text: "执行次数: " + executeCount
            }

            Label {
                text: "周积分设置:"
                font.pixelSize: 12
                color: "gray"
            }

            GridLayout {
                columns: 7
                Layout.fillWidth: true

                Repeater {
                    model: 7

                    ColumnLayout {
                        spacing: 2

                        Label {
                            text: "第" + (index + 1) + "天"
                            font.pixelSize: 10
                            color: "gray"
                            Layout.alignment: Qt.AlignHCenter
                        }

                        TaskButton {
                            number: dayPoints[index]
                            completed: false
                            onClicked: {
                                console.log("clicked" + completed)
                                if (!completed) {
                                    completed = true;
                                    userManager.pointsIncrease(number);
                                    // 使用 ListView.view.model 来访问模型
                                    kidTaskModel.completeTask(parent.parent.parent.index, index);
                                    taskCompleted(number);
                                    console.log("sig send")
                                }
                            }
                        }
                    }
                }
            }
        }

        ColumnLayout {
            spacing: 5

            Button {
                text: "编辑"
                Layout.preferredWidth: 80
                onClicked: {
                    editTaskDialog.taskIndex = index
                    editTaskDialog.taskName = name
                    editTaskDialog.executeCount = executeCount
                    editTaskDialog.dayPoints = dayPoints
                    editTaskDialog.open()
                }
            }

            Button {
                text: "删除"
                Layout.preferredWidth: 80
                onClicked: {
                    // 使用 ListView.view.model 来访问模型
                    kidTaskModel.removeTask(index)
                }
            }
        }
    }
}
